package service

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"strconv"
	"web_framework/config"
	"web_framework/model"
	. "web_framework/pkg/e"
)

var httpSearchRe struct{
	Status       int                  `json:"status"`
	Info         string               `json:"info"`
	Data		 []map[string]string  `json:"data"`
}

var httpSearchDetailRe struct{
	Status int                      `json:"status"`
	Info   string                   `json:"info"`
	Data   model.ResourceDetailData `json:"data"`
}


func ResourceCloudSearch(uid, token, keyword string, page int) (re []model.ResourceSearch, err error) {

	// 请求的url
	url := "http://ios.zmzapi.com/index.php?g=api/v3&m=index&a=router_search&accesskey=" + config.C.YyetsV3Key
	// 准备参数
	url += "&k="+keyword + "&st=resource&limit=15&page="+strconv.Itoa(page)+"&uid="+uid+"&token="+token
	// 提交请求
	request, err := http.Get(url)
	if err != nil {
		Log("Info", "Http 请求失败", err.Error())
		return re, err
	}

	defer request.Body.Close()
	body, err := ioutil.ReadAll(request.Body)
	if err != nil {
		Log("Info", "Http 读取数据失败", err.Error())
		return re, err
	}

	err = json.Unmarshal(body, &httpSearchRe)
	if err != nil {
		Log("Info", "Http 解析数据失败", err.Error())
		return re, err
	}

	if httpSearchRe.Status == 1 {
		for _, v := range httpSearchRe.Data {
			vid, _ := strconv.Atoi(v["id"])
			re = append(re, model.ResourceSearch{
				Vid: vid,
				Name: v["cnname"],
			})
		}
	}else {
		Log("Info", "Http 请求接口 [api/v3&m=index&a=router_search] 失败")
	}

	return re, nil
}

// 影视详情接口
func HttpSearchResourceDetail(rid, uid, token, client string) (re model.ResourceDetailData, err error) {

	// 请求的url
	url := "http://ios.zmzapi.com/index.php?g=api/v3&m=index&a=router_resource&accesskey="
	// 准备参数
	url += config.C.YyetsV3Key + "&id="+rid+"&uid="+uid+"&token="+token+"&client"+client
	// 提交请求
	request, err := http.Get(url)
	if err != nil {
		Log("Info", "Http 请求接口失败", err.Error())
		return re, err
	}

	defer request.Body.Close()
	body, err := ioutil.ReadAll(request.Body)
	if err != nil {
		Log("Info", "Http 请求读取数据失败", err.Error())
		return re, err
	}

	err = json.Unmarshal(body, &httpSearchDetailRe)
	if err != nil {
		Log("Info", "Http 请求解析数据失败", err.Error())
		return re, err
	}

	var listData []model.ResourceDetailDataList
	if httpSearchDetailRe.Status == 1 {
		re.Id = httpSearchDetailRe.Data.Id
		re.Channel = httpSearchDetailRe.Data.Channel
		re.ChannelCn = httpSearchDetailRe.Data.ChannelCn
		re.CnName = httpSearchDetailRe.Data.CnName
		re.EnName = httpSearchDetailRe.Data.EnName
		re.Category = httpSearchDetailRe.Data.Category
		re.Poster = httpSearchDetailRe.Data.Poster

		for _, v := range httpSearchDetailRe.Data.List {
			var seasonDetail model.ResourceDetailDataList
			var episodesDetail model.EpisodesDetail
			var episodesDetailList []model.EpisodesDetail
			seasonDetail.Season = v.Season
			seasonDetail.SeasonName = v.SeasonName
			for _, vv := range v.Episodes {
				episodesDetail.Episode = vv.Episode
				episodesDetail.EpisodeName = vv.EpisodeName
				episodesDetail.Url = vv.Url
				episodesDetailList = append(episodesDetailList, episodesDetail)
			}
			seasonDetail.Episodes = episodesDetailList
			listData = append(listData, seasonDetail)
		}
		re.List = listData
	}

	return re, nil
}
